import { Request, Response } from 'express';
import { ILessonDocument, Lesson } from '../models';

export const list = async (req: Request, res: Response) => {
    let { category } = req.query;
    let offset: any = req.query.offset;
    let limit: any = req.query.limit;
    offset = isNaN(offset) ? 0 : parseInt(offset); //偏移量
    limit = isNaN(limit) ? 5 : parseInt(limit); //每页条数
    const query: Partial<ILessonDocument> = {} as ILessonDocument;
    if(category && category != 'all'){
        query.category = category as string
    }
    const total = await Lesson.count(query);
    const list = await Lesson.find(query).sort({order:1}).skip(offset).limit(limit);
    res.json({
        code: 0,
        success: true,
        data: {
            list,
            hasMore: total > offset + limit
        }
    });
}

export const getById = async (req: Request, res: Response) => {
    const id = req.params.id;
    const lesson = await Lesson.findById(id);
    res.json({
        success: true,
        data: lesson
    })
}